package com.lib.module_base.utils

import android.os.Build
import android.os.Handler
import android.os.Looper
import androidx.annotation.RequiresApi
import java.text.SimpleDateFormat
import java.time.LocalDate
import java.time.Period
import java.time.format.DateTimeFormatter
import java.time.format.DateTimeParseException
import java.util.Calendar
import java.util.Locale


/**
 * 工具类
 */

val mainHandler by lazy { Handler(Looper.getMainLooper()) }


@RequiresApi(Build.VERSION_CODES.O)
fun isValidDate(dateString: String): Boolean {
    return try {
        val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
        LocalDate.parse(dateString, formatter)
        true
    } catch (e: DateTimeParseException) {
        false
    }
}

@RequiresApi(Build.VERSION_CODES.O)
fun isUnderEighteen(dateString: String): Boolean {
    val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
    val birthDate = LocalDate.parse(dateString, formatter)
    val currentDate = LocalDate.now()
    val age = Period.between(birthDate, currentDate).years
    return age < 18
}

//获取当前年
fun getCurrentYear():String{
    val calendar = Calendar.getInstance()
    val year = calendar.get(Calendar.YEAR)
    return year.toString()
}

//获取当前月
fun getCurrentMonth(): String {
    val calendar = Calendar.getInstance()
    // 格式化月份为英文缩写形式
    val dateFormat = SimpleDateFormat("MMM", Locale.ENGLISH)
    return dateFormat.format(calendar.time)
}

//获取当前日
fun getCurrentDay(): String {
    val calendar = Calendar.getInstance()
    val dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH)
    if(dayOfMonth>=10){
        return dayOfMonth.toString()
    }
    return "0$dayOfMonth"
}